﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace SuperX.PermissionsService.Data
{
    public class IdentityRepository
    {
        public Account Account { get; set; }

        private readonly IdentityDbContext dbContext;

        public IdentityRepository()
        {
            dbContext = new IdentityDbContext();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool Login(string userName, string password)
        {
            if (dbContext.Accounts.IsAny(d => d.UserName == userName))
            {
                var account = dbContext.Accounts.GetSingle(d => d.UserName == userName);
                var _password = SHA256Helper.SHA256Encrypt(password);
                if (account.Password == _password)
                {
                    Account = account;
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 获取账号
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public Account GetAccount(string userName)
        {
            if (dbContext.Accounts.IsAny(d => d.UserName == userName))
            {
                var user = dbContext.Accounts.GetSingle(d => d.UserName == userName);
                Account = user;
                return user;
            }
            return null;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="roleName">角色名</param>
        public bool Register(string userName, string name, string password,string roleName)
        {
            var account = new Account()
            {
                UserName = userName,
                Name = name,
                Password = SHA256Helper.SHA256Encrypt(password),
                RoleName = roleName,
                RegisterTime = DateTime.Now,
            };
            return dbContext.Accounts.Insert(account);
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="userName">用户名</param>
        public bool Unregister(string userName)
        {
            if (dbContext.Accounts.IsAny(d => d.UserName == userName))
            {
                var account = dbContext.Accounts.GetSingle(d => d.UserName == userName);
                return dbContext.Accounts.Delete(account);
            }
            return false;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="name">姓名</param>
        /// <param name="password">密码</param>
        public bool ChangePassword(string userName, string password)
        {
            if (dbContext.Accounts.IsAny(d => d.UserName == userName))
            {
                var account = dbContext.Accounts.GetSingle(d => d.UserName == userName);
                account.Password = SHA256Helper.SHA256Encrypt(password);
                return dbContext.Accounts.Update(account);
            }
            return false;
        }

        /// <summary>
        /// 设置账号角色
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public bool SetAccountRole(string userName, string roleName)
        {
            if (dbContext.Accounts.IsAny(d => d.UserName == userName))
            {
                var account = dbContext.Accounts.GetSingle(d => d.UserName == userName);
                if (dbContext.Roles.IsAny(d => d.Name == roleName))
                {
                    account.RoleName = roleName;
                    return dbContext.Accounts.Update(account);
                }
            }
            return false;
        }

        /// <summary>
        /// 获取所有账号
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">页码大小</param>
        /// <returns></returns>
        public List<Account> GetAllAccounts(int page,int pageSize)
        {
            return dbContext.Accounts.AsQueryable().Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 获取所有账号的数量
        /// </summary>
        /// <returns></returns>
        public int GetAccountCount()
        {
            return dbContext.Accounts.AsQueryable().Count();
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="description">描述</param>
        /// <param name="functionMenu">功能菜单</param>
        public bool CreateRole(string roleName, string description = null, string functionMenu = null)
        {
            var role = new Role()
            {
                Name = roleName,
                Description = description,
                FunctionMenu = functionMenu
            };
            return dbContext.Roles.Insert(role);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public bool DeleteRole(string roleName)
        {
            if (dbContext.Roles.IsAny(d => d.Name == roleName))
            {
                var role = dbContext.Roles.GetSingle(d => d.Name == roleName);
                return dbContext.Roles.Delete(role);
            }
            return false;
        }

        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <param name="description">描述</param>
        /// <param name="functionMenu">功能菜单</param>
        /// <returns></returns>
        public bool UpdateRole(string roleName, string description = null, string functionMenu = null)
        {
            if (dbContext.Roles.IsAny(d => d.Name == roleName))
            {
                var role = dbContext.Roles.GetSingle(d => d.Name == roleName);
                role.Description = description;
                role.FunctionMenu = functionMenu;
                return dbContext.Roles.Update(role);
            }
            return false;
        }

        /// <summary>
        /// 获取角色
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public Role GetRole(string roleName)
        {
            if (dbContext.Roles.IsAny(d => d.Name == roleName))
            {
                return dbContext.Roles.GetSingle(d => d.Name == roleName);
            }
            return null;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">页码大小</param>
        /// <returns></returns>
        public List<Role> GetAllRoles(int page, int pageSize)
        {
            return dbContext.Roles.AsQueryable().Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        /// <summary>
        /// 获取所有角色的数量
        /// </summary>
        /// <returns></returns>
        public int GetRoleCount()
        {
            return dbContext.Roles.AsQueryable().Count();
        }
    }
}